// 定义懒加载插件
import { useIntersectionObserver } from '@vueuse/core'

export const lazyPlugin = {
    install(app) {
        // 定义全局指令
        app.directive('img-lazy', {
            //el：指令绑定的那个元素 img
            //binding:binding.value 图片url
            mounted(el, binding) {
                console.log(el, binding.value)
                const { stop } = useIntersectionObserver(
                    el,
                    ([{ isIntersecting }]) => {
                        console.log(isIntersecting)
                        if (isIntersecting) {
                            // 进入视图
                            el.src = binding.value
                            stop()
                        }
                    }
                )
            }
        })
        console.log(app)
    }
}
